package com.itheima.test2;

import java.util.Random;
import java.util.Scanner;

public class StringDemo8 {
    public static void main(String[] args) {
        // 键盘录入一个字符串，并打乱里面的内容

        // 1.键盘录入
        Scanner scanner = new Scanner(System.in);

        System.out.println("请输入一个字符串:");
        String str = scanner.next(); //输入：1234abc

        String result = disrupt(str);
        System.out.println(result);//打印结果：1a24c3b

    }

    // 打乱字符串内容
    public static String disrupt(String str) {
        char[] arr = str.toCharArray();//把字符串转数组字符
        Random random = new Random();//实例化随机数对象
        for (int i = 0; i < arr.length; i++) {
            int randomIndex = random.nextInt(arr.length);//生成随机数
            char temp = arr[randomIndex];
            arr[randomIndex] = arr[i];
            arr[i] = temp;
        }

        return new String(arr);
    }

}
